package cn.edu.tju.wiki.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Categorize {
	private static final String[] keySet = { "sd_name", "sd_era", "sd_discover_time", "sd_discover_site", "sd_authority"};
	private static final boolean [] isEnable = {true, true, true, true, false, false, false};
	private static final String [] CateSet = {"朝代", "器型", "收藏", "出土遗址", "纹饰", "出土时间", "其他"};
	private static final String [][] subCateSet = {{"商代", "西周", "春秋", "戰國"}, {"食器", "酒器", "水器", "乐器", "兵器"}, {"中国", "国外"}, {"中国各省份"}, {}, {}, {}};
	private static final String [][][] subSubCateSet = {{{"商代中期", "商代晚期", "商代晚期或西周早期"},
				{"西周", "西周早期", "西周中期", "西周晚期", "西周早期或中期", "西周中期或晚期", "西周晚期或春秋早期"},
				{"春秋", "春秋早期", "春秋中期", "春秋晚期", "春秋戰國", "春秋中期或晚期", "春秋晚期或戰國早期"},
				{"戰國", "戰國早期", "戰國中期", "戰國早期或中期", "戰國中期或晚期", "戰國晚期"}},
			{{"鼎", "鬲", "甗", "簋", "簠", "盨", "敦", "豆"},
				{"爵", "角", "斝", "觚", "觶", "觥", "尊", "卣", "盉", "方彝", "勺"},
				{"罍", "壺", "盤", "匜", "瓿", "盂", "盉", "鑑"},
				{"鐃", "鐘", "鎛", "鼓座", "錞", "鉦鋮", "鐃", "鐸", "鎛鐘", "甬鐘", "钮钟"},
				{"鉞", "戈", "矛", "戟", "劍", "鈹", "鏃", "盾钖"}},
				{{"上海", "北京", "南京", "台北", "天津", 
					"內蒙古", "吉林", "四川", "安徽", "山東", "山西", "廣東", "廣西", "江蘇", "江西",
					"河北", "河南", "浙江", "湖北", "湖南", "澳門", "甘肅", "遼寧", "陝西", "香港"},
					{"美國", "丹麥", "倫敦", "加拿大", "德國", "新加坡", "比利時", "日本",
							"法國", "澳大利亞", "瑞典", "瑞士", "義大利", "英國", "荷蘭"}},
				{{"上海", "北京", "南京", "台北", "天津", 
				"內蒙古", "吉林", "四川", "安徽", "山東", "山西", "廣東", "廣西", "江蘇", "江西",
				"河北", "河南", "浙江", "湖北", "湖南", "澳門", "甘肅", "遼寧", "陝西", "香港"}}
			};
	
	private static List<Map<String, Object>> cateLst = new ArrayList<Map<String, Object>>();
	private static List<Integer> numLst = new ArrayList<Integer>();
	
	public static void main(String[] args) throws Exception {
		String resultStr = "名称,";
		for (int i = 0; i < isEnable.length; i++) {
			if(isEnable[i]){
				resultStr += CateSet[i] + ",";
			}
		}
		resultStr = resultStr.substring(0, resultStr.length()-1);
		resultStr = "species,朝代,器型,收藏,出土,num";//"species,sepal length,sepal width,petal length,petal width";
		for (int i = 0; i < Constant.dataList.size(); i++) {
			Map<String, Object> newItem = new HashMap<String, Object>();
			newItem.put("eraStr", (String)Constant.dataList.get(i).get(keySet[1]).toString().split("（")[0].split("\\(")[0]);
			newItem.put("era", getSerilNum((String)Constant.dataList.get(i).get(keySet[1]).toString().split("（")[0].split("\\(")[0], 0));
			newItem.put("shape", getSerilNum((String)Constant.dataList.get(i).get(keySet[0]), 1));
			newItem.put("authority", getSerilNum((String)Constant.dataList.get(i).get(keySet[4]), 2));
			newItem.put("site", getSerilNum((String)Constant.dataList.get(i).get(keySet[3]), 3));
			if(cateLst.contains(newItem)){
				int temIndex = cateLst.indexOf(newItem);
				numLst.set(temIndex, numLst.get(temIndex) +1) ;
				continue;
			}else{
				cateLst.add(newItem);
				numLst.add(new Integer(1));
			}
		}
		
		for (int i = 0; i < cateLst.size(); i++) {
			resultStr += "\n"+ cateLst.get(i).get("eraStr") + "," 
					+ cateLst.get(i).get("era") + "," 
					+ cateLst.get(i).get("shape") + "," 
					+ cateLst.get(i).get("authority") + "," 
					+ cateLst.get(i).get("site") + "," 
					+ (numLst.get(i)<=10 ? numLst.get(i): Math.log10(numLst.get(i))*10);
			
		}
		System.out.println(resultStr);
		GenJsonJsFile.writeFile("/Users/liudajian/Workspaces/MyEclipse 10/CH/WebRoot/d3-pages/wikidata/data/parall.csv", resultStr);
	}
	
	private static double getSerilNum(String target, int rank){
		double result = 0;
		int index = 0;
		int ceil = 0;
		for (int i = 0; i < subSubCateSet.length; i++) {
			if(rank == index){
				for (int j = 0; j < subSubCateSet[i].length; j++) {
					if(subSubCateSet[i][j].length > ceil){
						ceil = subSubCateSet[i][j].length;
					}
				}
				for (int j = 0; j < subSubCateSet[i].length; j++) {
					for (int j2 = subSubCateSet[i][j].length-1; j2 > 0; j2--) {
						if(target.contains(subSubCateSet[i][j][j2])){
							result =  ceil * j + j2;
							break;
						}
					}
				}
			}else{
				if(isEnable[i]){
					index ++;
				}
			}
		}
		return result/ceil;
	}
}
